package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.PermissionVo;
import com.moli.iov.admin.model.vo.req.RoleVo;
import com.moli.iov.admin.service.IPermissionFacadeService;
import com.moli.iov.admin.service.IRoleFacadeService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.enums.RoleTypeEnum;
import com.moli.iov.sys.enums.UserTypeEnum;
import com.moli.iov.sys.model.dto.req.RolePermissionDto;
import com.moli.iov.sys.model.po.Permission;
import com.moli.iov.sys.model.po.Role;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色管理
 * </p>
 *
 * @author zql
 * @since 2019/9/6 16:31
 */
@Slf4j
@RestController
@RequestMapping("/auth")
public class RoleContorller extends BaseController {

  @Autowired
  public IRoleFacadeService roleFacadeService;

  @Autowired
  private IPermissionFacadeService permissionFacadeService;

  /**
   * 添加角色
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/platform/add")
  public RestResponse saveRole(@RequestBody @Validated({RoleVo.Save.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    roleVo.setRoleType(RoleTypeEnum.PLATFORM.getFlag());
    if(UserTypeEnum.COMPANY.getFlag() == roleVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    return roleFacadeService.saveRole(roleVo);
  }

  /**
   * 修改角色
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/platform/update")
  public RestResponse updateRoleByRoleId(@RequestBody @Validated({RoleVo.Update.class})RoleVo roleVo){
    assembleUserInfo(roleVo);
    if(UserTypeEnum.COMPANY.getFlag() == roleVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    return roleFacadeService.updateRoleByRoleId(roleVo);
  }

  /**
   *
   * 分页查询角色列表
   *
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/platform/list")
  public RestResponse<PageExtend<Role>> listRoleOfPage(@RequestBody(required = false) RoleVo roleVo){
    if(roleVo == null){
      roleVo = new RoleVo();
    }
    assembleUserInfo(roleVo);
    roleVo.setRoleType(RoleTypeEnum.PLATFORM.getFlag());
    if(UserTypeEnum.COMPANY.getFlag() == roleVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    return roleFacadeService.listRoleOfPage(roleVo);
  }

  /**
   *
   * 查询角色列表
   *
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/platform/listRole")
  public RestResponse<List<Role>> listRole(@RequestBody(required = false) RoleVo roleVo){
    if(roleVo == null){
      roleVo = new RoleVo();
    }
    assembleUserInfo(roleVo);
    if(UserTypeEnum.COMPANY.getFlag() == roleVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    return roleFacadeService.listRole(roleVo);
  }

  /**
   * 设置权限
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/platform/setPermission")
  public RestResponse setPermission(@RequestBody @Validated({RoleVo.Set.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    if(UserTypeEnum.COMPANY.getFlag() == roleVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    return roleFacadeService.setPermission(roleVo);
  }

  /**
   * 权限详情
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/platform/getRolePermissions")
  public RestResponse<RolePermissionDto> getRolePermissions(@RequestBody @Validated({
      RoleVo.GetRolePermissions.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    if(UserTypeEnum.COMPANY.getFlag() == roleVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    return roleFacadeService.getRolePermissions(roleVo);
  }


  /**
   * 添加角色
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/company/add")
  public RestResponse saveRoleOfCompany(@RequestBody @Validated({RoleVo.Save.class}) RoleVo roleVo){

    assembleUserInfo(roleVo);

    roleVo.setRoleType(RoleTypeEnum.COMPANY.getFlag());

    if(UserTypeEnum.PLATFORM.getFlag() == roleVo.getSysUserType() && roleVo.getCompanyId() == null){
      return RestResponse.failure("企业id不能为空");
    }

    if(roleVo.getCompanyId() == null && roleVo.getSysUserType() == UserTypeEnum.COMPANY.getFlag()){
      roleVo.setCompanyId(roleVo.getSysCompanyId());
      roleVo.setCompanyName(roleVo.getSysCompanyName());
    }

    return roleFacadeService.saveRole(roleVo);
  }

  /**
   * 修改角色
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/company/update")
  public RestResponse updateRoleByRoleIdOfCompany(@RequestBody @Validated({RoleVo.Update.class})RoleVo roleVo){
    assembleUserInfo(roleVo);
    return roleFacadeService.updateRoleByRoleId(roleVo);
  }

  /**
   *
   * 分页查询角色列表
   *
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/company/list")
  public RestResponse<PageExtend<Role>> listRoleOfPageOfCompany(@RequestBody(required = false) RoleVo roleVo){
    if(roleVo == null){
      roleVo = new RoleVo();
    }
    assembleUserInfo(roleVo);
    roleVo.setRoleType(RoleTypeEnum.COMPANY.getFlag());
    if(UserTypeEnum.COMPANY.getFlag() == roleVo.getSysUserType()){
      roleVo.setCompanyId(roleVo.getSysCompanyId());
    }
    return roleFacadeService.listRoleOfPage(roleVo);
  }

  /**
   *
   * 查询角色列表
   *
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/company/listRole")
  public RestResponse<List<Role>> listRoleOfCompany(@RequestBody(required = false) RoleVo roleVo){
    if(roleVo == null){
      roleVo = new RoleVo();
    }
    assembleUserInfo(roleVo);
    return roleFacadeService.listRole(roleVo);
  }

  /**
   * 设置权限
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/company/setPermission")
  public RestResponse setPermissionOfCompany(@RequestBody @Validated({RoleVo.Set.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    return roleFacadeService.setPermission(roleVo);
  }

  /**
   * 权限详情
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/company/getRolePermissions")
  public RestResponse<RolePermissionDto> getRolePermissionsOfCompany(@RequestBody @Validated({
      RoleVo.GetRolePermissions.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    return roleFacadeService.getRolePermissions(roleVo);
  }

  /**
   * 根据用户类型查询全部权限
   * @return
   */
  @RequestMapping("/permission/listAllPermissionNeApi")
  public RestResponse<List<Permission>> listAllPermissionNeApi(){
    PermissionVo permissionVo = new PermissionVo();
    assembleUserInfo(permissionVo);
    return permissionFacadeService.listAllPermissionNeApi(permissionVo);
  }

  /**
   * 根据用户Id查询角色列表
   * @param roleVo
   * @return
   */
  @RequestMapping("/role/listRolesByUserId")
  public RestResponse<List<Role>> listRolesByUserId(@RequestBody(required = false) RoleVo roleVo){
    if(roleVo == null){
      roleVo = new RoleVo();
    }

    assembleUserInfo(roleVo);

    return roleFacadeService.listRolesByUserId(roleVo);
  }

}
